<template>
  <div class="loginPage">
    <div class="headInfo">
      <div class="bg-blur"></div>
      <div class="text">
        <img src="@/assets/logo.png" alt="logo" />
        “数智社事”内部管理系统
      </div>
    </div>
    <div class="leftBox"></div>
    <div class="rightBox">
      <div class="contentBox">
        <div class="title">登录</div>
        <div class="loginFormBox">
          <Transition :enter-active-class="changeCss()">
            <component :is="whichComponent" @onChange="change" ref="loginForm"></component>
          </Transition>
        </div>
        <el-button :loading="isLoading" class="loginBtnBox" color="#F0F0F0" @click="loginSubmit">登录</el-button>
        <div class="loginTypeBtnBox" @click="componentChange(loginType)">
          {{loginType}}
        </div>
      </div>
    </div>

  </div>
</template>

<script setup>
import { reactive, ref, shallowRef } from "vue";
import { useRouter } from "vue-router";
import UserNameForm from "./components/userNameForm.vue";
import TelForm from "./components/telForm.vue";

const router = useRouter();

const isLoading = ref(false);

const state = reactive({
  cssArr: ['animated rollIn', 'animated zoomIn', 'animated bounceIn', 'animated jello', 'animated wobble', 'animated rubberBand']
});
const changeCss = () => {
  const i = Math.floor(Math.random() * 5)
  return state.cssArr[i]
}


// 这里用ref的话，vue给出警告Vue接收到一个组件，该组件被制成反应对象，可能会导致不必要的性能开销。
// 应该通过将组件标记为 “markRaw” 或使用 “shallowRef” 而不是 “ref” 来避免。
// 如果使用 markRaw 那么 whichComponent 将永远不会再成为响应式对象。 所以得使用 shallowRef。
// shallowRef用于不需要深度监听的数据。
let whichComponent = shallowRef(UserNameForm)
let loginType = ref('短信验证登录')
const componentChange = (type) => {
  if (type === '短信验证登录') {
    loginType.value = '用户名密码登录'
    whichComponent.value = TelForm
  } else {
    loginType.value = '短信验证登录'
    whichComponent.value = UserNameForm
  }
}

// 此名称必须与子组件中的ref值名称保持一致才能获取到子组件中的方法
let loginForm = ref(null)

/**
 * 登录按钮
 */
const change = (val) => {
    isLoading.value = val;
}

// 提交-调用子组件方法
const loginSubmit = () => {
  loginForm.value.submitForm()
};
</script>

<style lang="scss" scoped>
.loginPage {
  width: 100vw;
  height: 100vh;
  overflow: hidden;
  display: flex;
  .headInfo {
    position: absolute;
    top: 0;
    width: 100%;
    height: 70px;
    font-size: 18px;
    color: #1a1a1a;
    display: flex;
    justify-content: center;
    align-items: center;
    box-shadow: 0px 1px 4px 0px #ccc;
    border-radius: 0 0 20px 20px;
    .text {
      position: absolute;
      img {
        width: 27px;
        height: 29px;
        vertical-align: middle;
      }
    }
    .bg-blur {
      width: 100%;
      height: 100%;
      background: #fff;
      opacity: 0.5;
      border-radius: 0 0 20px 20px;
    }
  }
  .leftBox {
    flex: 1;
    height: 100%;
    //background: no-repeat center url('http://rsc71m4tf.hn-bkt.clouddn.com/index.png');
    background: no-repeat center url('https://s1.ax1x.com/2023/06/29/pCwrLYq.jpg');
    background-size: 100% 100%;
  }
  .rightBox {
    width: 755px;
    height: 100%;
    display: flex;
    justify-content: center;
    .contentBox {
      width: 70%;
      margin-top: 252px;
      .title {
        text-align: left;
        font-size: 38px;
        font-weight: bold;
        color: #1A1A1A;
        margin-bottom: 42px;
      }
      .loginFormBox {
        width: 100%;
      }
      .loginBtnBox {
        width: 100%;
        height: 52px;
        background: $title-sm-color;
        border-radius: 6px;
        font-size: 20px;
        font-weight: 500;
        color: #FFFFFF;
        margin-bottom: 20px;
      }
      .loginTypeBtnBox {
        text-align: right;
        font-size: 18px;
        font-weight: 500;
        color: $title-sm-color;
        cursor: pointer;
      }
    }
  }
}
</style>
